from main import db
from main import app
from sqlalchemy import Column, String, Integer, Boolean, Float, ForeignKey, DateTime, Date
from sqlalchemy.orm import relationship
from flask_sqlalchemy import SQLAlchemy
import json
from datetime import datetime, date
from main.utils import TimeHelper
from flask import Flask
import config


class User(db.Model):
    '''用户'''
    __tablename__ = "user"
    __table_args__ = {'extend_existing': True}

    id = Column(Integer(), primary_key=True)
    openid = Column(String(31))
    username = Column(String(31))
    password = Column(String(31))
    nick = Column(String(31))
    avatar = Column(String(255))
    identity = Column(Integer)  # user identity 0-user 1-admin 2-checker
    status = Column(Boolean, default=True)
    create_time = Column(DateTime, default=datetime.now())

    def to_json(self):
        dict = self.__dict__
        if "create_time" in dict:
            # print(type(dict["create_time"]))
            dict["create_time"] = TimeHelper.datetime_toString_short(dict["create_time"])
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class Lab(db.Model):
    '''lab'''
    __tablename__ = "lab"
    __table_args__ = {'extend_existing': True}

    id = Column(Integer(), primary_key=True)
    borrow_status = Column(Integer, default=0)
    name = Column(String(31))
    image = Column(String(255))
    status = Column(Boolean, default=True)
    create_time = Column(DateTime, default=datetime.now())

    def to_json(self):
        dict = self.__dict__
        if "create_time" in dict:
            # print(type(dict["create_time"]))
            dict["create_time"] = TimeHelper.datetime_toString_short(dict["create_time"])
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class Equipment(db.Model):
    '''equipment'''
    __tablename__ = "equipment"
    __table_args__ = {'extend_existing': True}

    id = Column(Integer(), primary_key=True)
    borrow_status = Column(Boolean, default=False)
    name = Column(String(31))
    image = Column(String(255))
    status = Column(Boolean, default=True)
    create_time = Column(DateTime, default=datetime.now())

    def to_json(self):
        dict = self.__dict__
        if "create_time" in dict:
            # print(type(dict["create_time"]))
            dict["create_time"] = TimeHelper.datetime_toString_short(dict["create_time"])
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class EquipmentUsed(db.Model):
    '''lab subscribe'''
    __tablename__ = "equipment_used"
    __table_args__ = {'extend_existing': True}

    id = Column(Integer(), primary_key=True)
    examine_status = Column(Integer, default=0)  # 0-subscribed 1-used 2-reject
    examine_id = Column(Integer, ForeignKey('user.id'))
    equipment_id = Column(Integer, ForeignKey('equipment.id'))
    subscribe_id = Column(Integer, ForeignKey('user.id'))
    status = Column(Boolean, default=True)
    start_time = Column(Date)
    end_time = Column(Date)
    create_time = Column(DateTime, default=datetime.now())

    def to_json(self):
        dict = self.__dict__
        if "create_time" in dict:
            dict["create_time"] = TimeHelper.datetime_toString_short(dict["create_time"])
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class LabSubscribe(db.Model):
    '''lab subscribe'''
    __tablename__ = "lab_subscribe"
    __table_args__ = {'extend_existing': True}

    id = Column(Integer(), primary_key=True)
    examine_status = Column(Integer, default=0)  # 0-subscribed 1-used 2-reject
    examine_id = Column(Integer, ForeignKey('user.id'))
    lab_id = Column(Integer, ForeignKey('lab.id'))
    subscribe_id = Column(Integer, ForeignKey('user.id'))
    status = Column(Boolean, default=True)
    start_time = Column(DateTime)
    end_time = Column(Date)
    create_time = Column(DateTime, default=datetime.now())

    def to_json(self):
        dict = self.__dict__
        if "create_time" in dict:
            dict["create_time"] = TimeHelper.datetime_toString_short(dict["create_time"])
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


# create tables
db.create_all()

if __name__ == '__main__':
    # create tables
    # db.create_all()
    app.run(debug=True)
